package log

import (
	"device-admin/internal/define"
	"encoding/json"
)

// fieldsTrans key字段翻译
func fieldsTrans(
	operationType define.OperationType, // 操作类型
	lang string, // 语言
	data string, // 待翻译的json数据
) string {
	// filedMap
	var fieldMap, transFieldMap map[string]string
	// json反序列化
	err := json.Unmarshal([]byte(data), &fieldMap)
	if err != nil {
		return ""
	}
	// 默认语言
	if lang == "" {
		lang = "zh-cn"
	}
	// 字段翻译
	if operationFieldLangDefine, ok := define.OperationFieldTrans[operationType]; ok {
		if operationFiledDefine, ok := operationFieldLangDefine[lang]; ok {
			transFieldMap = make(map[string]string)
			for key, value := range fieldMap {
				if trans, ok := operationFiledDefine[key]; ok {
					transFieldMap[trans] = value
				}
			}
		}
	}
	transFieldMapJson, _ := json.Marshal(transFieldMap)
	return string(transFieldMapJson)
}
